package com.lagou.config;

/**
 * @author Nocturne
 * @date 2021/5/10 9:39
 * @description 创建容器，存储动态选择的key
 * 5. 问题：如何存储动态选择的key？
 * 6. 创建RoutingDataSourceContext：通过threadlocal来存储datasource的key
 */
public class RoutingDataSourceContext {
	static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

	/** 存储key      key: 指定数据源类型   master   slave */
	public RoutingDataSourceContext(String key) {
		threadLocal.set(key);
	}

	/** 获取key */
	public static String getDataSourceRoutingKey() {
		String key = threadLocal.get();
		return key == null ? "master" : key;
	}

	public void close() {
		threadLocal.remove();
	}
}
